package com.cy.utils;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * @author Administrator
 */
public class ImageUtil
{

    private static final Logger logger = LoggerFactory.getLogger(ImageUtil.class);

    /**
     * 将图片编码为base64
     *
     * @param url 图片的url地址
     * @return 返回Base64编码过的字节数组字符串
     * @throws IOException
     */
    public static String imageToBase64(String url) throws IOException
    {
        InputStream inStream = null;
        try
        {
            URL urlContent = new URL(url);
            HttpURLConnection con = (HttpURLConnection) urlContent.openConnection();
            con.setRequestMethod("GET");
            con.setConnectTimeout(5 * 1000);
            //通过输入流获取图片数据
            inStream = con.getInputStream();
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[2048];
            int len = 0;
            while ((len = inStream.read(buffer)) != -1)
            {
                outStream.write(buffer, 0, len);
            }
            inStream.close();
            byte[] data = outStream.toByteArray();
            Base64 base64 = new Base64();
            return base64.encodeToString(data);
        }
        catch (Exception e)
        {
            logger.error(e.getMessage());
        }
        finally
        {
            if (inStream != null)
            {
                try
                {
                    inStream.close();
                }
                catch (Exception e)
                {
                    logger.error("关闭InputStream失败：", e);
                }
            }
        }
        return null;
    }

    /**
     * 从http地址中获取文件字节流
     *
     * @param url 图片的url
     * @return 字节数组
     */
    public static byte[] getImgByte(String url)
    {
        InputStream inStream = null;
        try
        {
            URL httpUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            //通过输入流获取图片数据
            inStream = conn.getInputStream();
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inStream.read(buffer)) != -1)
            {
                outStream.write(buffer, 0, len);
            }
            return outStream.toByteArray();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage());
        }
        finally
        {
            if (inStream != null)
            {
                try
                {
                    inStream.close();
                }
                catch (Exception e)
                {
                    logger.error("关闭InputStream失败：", e);
                }
            }
        }
        return null;
    }
}
